/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * Create a new page that let a user filling up a survey
 */
qx.Class.define("bip.userview.pages.FillSurveyPage", 
{
	extend: bip.views.Page,

	/*
	*****************************************************************************
	 EVENTS
	*****************************************************************************
	*/  
	events :
	{
		
		
	},

	/*
	*****************************************************************************
	 PROPERTIES
	*****************************************************************************
	*/ 
	properties: {
		
  	},

	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
	
	/**
	 * Construct the window
	 */	
 	construct: function(pageModel)
	{
		this.base(arguments,pageModel,this.tr("Fill survey"),this.tr("Fill survey"),null,"icon/48/apps/utilities-text-editor.png");
		
		var service = bip.services.Survey.getInstance();
		service.addListener("error",function(evt) {
			this.setStateWithDescription("error",evt.getData().getMessage());
		},this);
		service.addListener("loaded",function(evt) {
			this.__buildForm(evt.getData());
			//this.setStateWithDescription("ready","Ready");
		},this);
		service.addListener("loading",function(evt) {
			this.setStateWithDescription("running",evt.getData()+","+ this.tr("please wait..."));
		},this);
		
		qx.locale.Manager.getInstance().addListener("changeLocale", function(evt) {
			bip.services.Survey.getInstance().fetchSurvey();
		}, this);
		
		bip.services.Survey.getInstance().fetchSurvey();
	},

	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/	
	members:
	{	
		__model: null,
		__form: null,
		__scroll: null,
		__uiSurvey: null,
		
		/**
     * Create the content of the dialog. 
     * Extending classes must implement this method.
     */
    _createWidgetContent : function()
    {
    	this.__createActionBar(this.getChildControl("actionbar"));
    	this.setLayout(new qx.ui.layout.VBox(5));
    	// create scroll container
		  this.__scroll = new qx.ui.container.Scroll();
		  this.add(this.__scroll,{flex:1});
      
    },
    
    /**
		 * Fill up the controler with the button bar
		 */
    __createActionBar: function(control) {
			var saveCmd = new qx.ui.core.Command();
			saveCmd.addListener("execute",this.__onSaveButtonClickHandler,this);
			var reloadCmd = new qx.ui.core.Command();
			reloadCmd.addListener("execute",this.__onReloadButtonClickHandler,this);
			this.populateActionBar({
				buttons:[
							{id:"save", type:"SaveButton",label:"Save",command:saveCmd},
							{id:"reload", type:"RefreshButton", label: "Reload",command:reloadCmd}
						]
			},control);
			//this.setActionBarButtonsProperties({width:80,height:20});
		},
		
		__buildForm: function(model) {
			if (this.__form != null) {
				// Currently, we need to rebuild entirely the form in case of reloading
				// which means deleting all form conponents
				this.__form.dispose();
				this.__scroll.remove(this.__uiSurvey);
				this.__uiSurvey.dispose();
			}
			this.__form = new bip.userview.forms.FillSurveyForm();
			
			this.__form.addListener("changeState",function(evt) {
				if (evt.getData() == "new") {
					this.setStateWithDescription("ready",this.tr("Please answer to the survey questions"));
				}
				else if (evt.getData() == "on-progress") {
					this.setStateWithDescription("ready",this.tr("You did not answer to all questions, please complete the survey"));
				}
				else if (evt.getData() == "completed") {
					this.setStateWithDescription("ok",this.tr("The survey is ready to be analysed"));
					if (! this.__buildingForm) {
						dialog.Dialog.confirm(
							this.tr("The survey is completed: you need to save it before being able to analyse it<br/> Save it ?"),
							function(result) {
								if (result) {
									this.__onSaveButtonClickHandler();
								}
							},
							this
						);
					} else {
						dialog.Dialog.confirm(
							this.tr("The survey is ready to be analysed<br/> Analyse it ?"),
							function(result) {
								if (result) {
									bip.Application.getInstance().load("analyse_survey");
								}
							},
							this
						);
					}
				}
				else {
					this.setStateWithDescription("error","Unknown error");
				}
				this.__buildingForm = false;
			},this);
			
			this.__buildingForm = true;
			this.__form.build(model);
			
			// This one must be after the form build !
			this.__form.addListener("changeState",function(evt) {
				if (evt.getData() == "completed") {
					// Propose to save the survey
					
					
				}
			},this);
	  		var formController = new qx.data.controller.Form(null, this.__form,false);
	  		this.__model = formController.createModel();
	  		this.__uiSurvey = new bip.ui.form.survey.FillSurveyRenderer(this.__form);
	  		this.__scroll.add(this.__uiSurvey);
		},
		
		__onSaveButtonClickHandler: function(evt) {
			if (this.__form.validate()) {
				this.setStateWithDescription("running",this.tr("Saving survey, please wait..."));
				bip.services.Survey.getInstance().postSurvey(this.__model);
			} else {
				this.setStateWithDescription("error",this.tr("Please correct the error(s) on this form"));
			}
		},
		
		__onReloadButtonClickHandler: function(evt) {
			bip.services.Survey.getInstance().fetchSurvey();
		},
		
		_beforeShow: function() {
			//this.setStateWithDescription("ready",this.tr("Use this form to fill a new survey"));
			//this.maximize();
	    }
		
	    

		/*
	  	*****************************************************************************
	     UI
	  	*****************************************************************************
	 	*/
		

	},
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects("__model","__form","__scroll","__uiSurvey");
		
	}
	
});

